package cz.muni.fi.pa165.ddtroops.dto;

import cz.muni.fi.pa165.ddtroops.entities.Hero;
import cz.muni.fi.pa165.ddtroops.entities.Race;
import cz.muni.fi.pa165.ddtroops.entities.Skill;
import cz.muni.fi.pa165.ddtroops.entities.Squad;
import cz.muni.fi.pa165.ddtroops.entities.User;
import java.util.ArrayList;
import java.util.List;


/**
 * Class creating DTO from entity or vice versa.
 * @author Svoboda
 */
public class DTOFactory {

    public static HeroDTO createHeroDTO(Hero hero){
        HeroDTO heroDTO = new HeroDTO();
        
        heroDTO.user = createUserDTO(hero.getUser());
        heroDTO.name = hero.getName();
        heroDTO.belief = hero.getBelief();
        heroDTO.biography = hero.getBiography();
        heroDTO.charisma = hero.getCharisma();
        heroDTO.constitution = hero.getConstitution();
        heroDTO.dexterity = hero.getDexterity();
        heroDTO.gender = hero.getGender();
        heroDTO.id = hero.getId();
        heroDTO.intelligence = hero.getIntelligence();
        heroDTO.profession = hero.getProfession();
        heroDTO.race = createRaceDTO(hero.getRace());
        heroDTO.strength = hero.getStrength();
        heroDTO.wisdom = hero.getWisdom();
        heroDTO.xp = hero.getXp();

        List<SkillDTO> skillDTOs = new ArrayList<SkillDTO>();

        for (Skill s : hero.getSkills()) {
            skillDTOs.add(createSkillDTO(s));
        }

        heroDTO.skills = skillDTOs;

        return heroDTO;
    }

    public static SkillDTO createSkillDTO(Skill skill){
        SkillDTO skillDTO = new SkillDTO();

        skillDTO.description = skill.getDescription();
        skillDTO.id = skill.getId();
        skillDTO.minXP = skill.getMinXP();
        skillDTO.name = skill.getName();
        skillDTO.profession = skill.getProfession();

        return skillDTO;
    }

    public static RaceDTO createRaceDTO(Race race){
        RaceDTO raceDTO = new RaceDTO();

        raceDTO.charisma = race.getCharisma();
        raceDTO.constitution = race.getConstitution();
        raceDTO.description = race.getDescription();
        raceDTO.dexterity = race.getDexterity();
        raceDTO.id = race.getId();
        raceDTO.intelligence = race.getIntelligence();
        raceDTO.name = race.getName();
        raceDTO.strength = race.getStrength();
        raceDTO.wisdom = race.getWisdom();

        return raceDTO;
    }

    public static SquadDTO createSquadDTO(Squad squad){
        SquadDTO squadDTO = new SquadDTO();

        squadDTO.description = squad.getDescription();
        squadDTO.gold = squad.getGold();
        squadDTO.id = squad.getId();
        squadDTO.name = squad.getName();
        squadDTO.task = squad.getTask();
        squadDTO.leader = createHeroDTO(squad.getLeader());

        List<HeroDTO> heroDTOs = new ArrayList<HeroDTO>();

        for (Hero h : squad.getMembers()) {
            heroDTOs.add(createHeroDTO(h));
        }

        squadDTO.members = heroDTOs;

        return squadDTO;
    }
    
    public static UserDTO createUserDTO(User user){
        UserDTO userDTO = new UserDTO();
        
        userDTO.setId(user.getId());
        userDTO.setName(user.getName());
        userDTO.setPassword(user.getPassword());
        userDTO.setAuthority(user.getAuthority());
        
        return userDTO;
    }

    public static Hero createHero(HeroDTO heroDTO){
        Hero hero = new Hero();

        hero.setUser(createUser(heroDTO.getUser()));
        hero.setBelief(heroDTO.belief);
        hero.setBiography(heroDTO.biography);
        hero.setCharisma(heroDTO.charisma);
        hero.setConstitution(heroDTO.constitution);
        hero.setDexterity(heroDTO.dexterity);
        hero.setGender(heroDTO.gender);
        hero.setId(heroDTO.id);
        hero.setIntelligence(heroDTO.intelligence);
        hero.setName(heroDTO.name);
        hero.setProfession(heroDTO.profession);
        hero.setRace(createRace(heroDTO.race));
        hero.setStrength(heroDTO.strength);
        hero.setWisdom(heroDTO.wisdom);
        hero.setXp(heroDTO.xp);

        for (SkillDTO s : heroDTO.skills) {
            hero.addSkill(createSkill(s));
        }

        return hero;
    }

    public static Skill createSkill(SkillDTO skillDTO){
        Skill skill = new Skill();

        skill.setDescription(skillDTO.description);
        skill.setId(skillDTO.id);
        skill.setMinXP(skillDTO.minXP);
        skill.setName(skillDTO.name);
        skill.setProfession(skillDTO.profession);

        return skill;
    }

    public static Race createRace(RaceDTO raceDTO){
        Race race = new Race();

        race.setCharisma(raceDTO.charisma);
        race.setConstitution(raceDTO.constitution);
        race.setDescription(raceDTO.description);
        race.setDexterity(raceDTO.dexterity);
        race.setId(raceDTO.id);
        race.setIntelligence(raceDTO.intelligence);
        race.setName(raceDTO.name);
        race.setStrength(raceDTO.strength);
        race.setWisdom(raceDTO.wisdom);

        return race;
    }

    public static Squad createSquad(SquadDTO squadDTO){
        Squad squad = new Squad();

        squad.setDescription(squadDTO.description);
        squad.setGold(squadDTO.gold);
        squad.setId(squadDTO.id);

        squad.setName(squadDTO.name);
        squad.setTask(squadDTO.task);

        for (HeroDTO h : squadDTO.members) {
            squad.addMember(createHero(h));
        }
        squad.setLeader(createHero(squadDTO.leader));        
        return squad;
    }
    
    public static User createUser(UserDTO userDTO){
        User user = new User();

        user.setId(userDTO.getId());
        user.setName(userDTO.getName());
        user.setPassword(userDTO.getPassword());
        user.setAuthority(userDTO.getAuthority());
        
        return user;
    }
}
